<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cron
 *
 * @author enjaz1
 */
class cron extends cms_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
    }

    function send_sms_api() {
        $this->load->library('sms');
        $sql = $this->db->get_where('sms_message', array('type' => 'wait'))->row();
        if(!$sql)
            return;
        $name = $this->db->get_where('store_details', array('store_id' => $sql->store_id, 'name' => 'storename'))->row()->value;
        if ($sql) {
            $phones = $this->db->get_where('sms_phones', array('message_id' => $sql->id, 'type' => 'wait'))->result();
            if ($phones) {
                $this->db->set('start_date', date("Y-m-d H:i:s"));
                $this->db->set('type', 'sending');
                $this->db->where(array('id' => $sql->id));
                $this->db->update('sms_message');
                foreach ($phones as $phone) {
                    $send = $this->sms->send_sms($phone->phone, NULL, $sql->message);
                    if ($send == '1') {
                        $this->db->set('type', 'send');
                    } else {
                        $this->db->set('type', 'fail');
                        $this->db->set('error', 'message_sms_' . $send);
                    }
                    $this->db->set('send_date', date("Y-m-d H:i:s"));
                    $this->db->where(array('id' => $phone->id));
                    $this->db->update('sms_phones');
                }
                $this->db->set('type', 'finish');
                $this->db->set('end_date', date("Y-m-d H:i:s"));
                $this->db->where(array('id' => $sql->id));
                $this->db->update('sms_message');
            }
        }
    }

}

?>
